/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controldepuntos.clases;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author DIANA
 */
public class DataBase {
    Connection con;
    Statement stm;
    ResultSet rs;
    
    public Connection conectar() throws SQLException{
        try{
            String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
            "databaseName=DBControlPuntos;user=appPuntos; password=12345;";
            con=DriverManager.getConnection(connectionUrl);
            //JOptionPane.showMessageDialog(null, "Conexión Exitosa");
            if(con!=null){
                return con;
            }else{
                return null;
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(AdministradorVentanas.pantallaInicio, ex.getMessage()+"Error in conectar()","Error",JOptionPane.ERROR_MESSAGE);
            con.close();
            return null;
        }
    }
    public void cerrar(Connection con){
        try{
            if(con!=null){
                if(!con.isClosed()){
                    con.close();
                }
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage()+"Error Utilidades.cerrar(con)","Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    public void cerrar(Connection con, ResultSet rs){
        try{
            if(con!=null){
                if(!con.isClosed()){
                    con.close();
                    con=null;
                }
            }
            if(rs!=null){
                if(!rs.isClosed()){
                    rs.close();
                    rs=null;
                }
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage()+"Error Utilidades.cerrar(con, rs)","Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    public void cerrar(Connection con, ResultSet rs, Statement stm){
        try{
            if(con!=null){
                if(!con.isClosed()){
                    con.close();
                    con=null;
                }
            }
            if(rs!=null){
                if(!rs.isClosed()){
                    rs.close();
                    rs=null;
                }
            }
            if(stm!=null){
                if(!stm.isClosed()){
                    stm.close();
                    stm=null;
                }
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex.getMessage()+"Error Utilidades.cerrar(con, rs, stm)","Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
}
